package cn.gzsxt.music.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.gzsxt.music.mapper.LableMapper;
import cn.gzsxt.music.mapper.MapperSqlProvider;
import cn.gzsxt.music.mapper.SongMapper;
import cn.gzsxt.music.query.RoleQueryObject;
import cn.gzsxt.music.query.SongQueryObject;
import cn.gzsxt.music.service.SongService;

@Service
public class SongServiceImpl implements SongService {
	
	@Autowired
	private SongMapper mapper;
	
	@Autowired
	private LableMapper labelMapper;

	@Override
	public int addSong(Map<String, Object> params) {
		return mapper.addSong(params);
	}

	@Override
	public int deleteSong(String song_ids) {
		return mapper.deletcSong(song_ids);
	}

	@Override
	public int updateSong(Map<String, Object> params) {
		return mapper.updateSong(params);
	}
	
	@Override
	public Map<String, Object> selectDataByCondition(Map<String, Object> params) {
		Map<String, Object> dataGridMap = new HashMap<>();
		dataGridMap.put("total", mapper.selectTotalByCondition(params));
		dataGridMap.put("rows", getLabel(mapper.selectByCondition(params)));
		return dataGridMap;
	}
	
	public List<Map<String,Object>> getLabel(List<Map<String,Object>> params){
		List<Map<String,Object>> labelsMapper = labelMapper.getAllLables();
		Map<String,String> labels = new HashMap<String,String>();
		for (Map<String,Object> label : labelsMapper) {
			labels.put(Integer.toString((int)label.get("label_id")), (String)label.get("label_name"));
		}
		Iterator<Map<String, Object>> it = params.iterator();
		while (it.hasNext()) {
			StringBuffer sb = new StringBuffer();
			Map<String, Object> map = it.next();
			String label_id = (String) map.get("label_id");
			//System.out.println(label_id);
			String[] labelList = label_id.split(",");
			for (String ll : labelList) {
				sb.append(labels.get(ll.trim())+",");
			}
			sb.deleteCharAt(sb.length()-1);
			map.put("song_label", sb);
			Integer hit = ((int)map.get("song_download")*5 + (int)map.get("song_like")*4 +(int)map.get("song_play")*3)/100;
			map.put("song_hit", hit);
			//System.out.println(sb);
		}	
		return params;		
	}

}
